package com.kzd.gtgame.utils

import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Environment
import com.blankj.utilcode.util.EncryptUtils
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException

private const val key1 = "fsd213ewdsadqwe2121213edsad"
private const val key2 = "riowreopfdwrops21qe"

private var httpStamp = ""

val httpTime: String
    get() = System.currentTimeMillis().toString().apply {
        httpStamp = this
    }

fun httpSign1(vararg data: String?): String {
    val sb = StringBuilder()
    data.forEach {
        if (!it.isNullOrEmpty()) {
            sb.append(it)
        }
    }
    sb.append(httpStamp).append(key1)
    return EncryptUtils.encryptMD5ToString(
        sb.toString()
    ).lowercase()
}

fun httpSign2(vararg data: String?): String {
    val sb = StringBuilder()
    data.forEach {
        if (!it.isNullOrEmpty()) {
            sb.append(it)
        }
    }
    sb.append(httpStamp).append(key2)
    return EncryptUtils.encryptMD5ToString(
        sb.toString()
    ).lowercase()
}


fun saveImageToGallery(context: Context, bmp: Bitmap, fileName: String?): Boolean {
    val file = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),
        fileName)
    try {
        val fos = FileOutputStream(file)
        //通过io流的方式来压缩保存图片
        val isSuccess = bmp.compress(Bitmap.CompressFormat.PNG, 60, fos)
        fos.flush()
        fos.close()
        //保存图片后发送广播通知更新数据库
        context.sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)))
        return isSuccess
    } catch (e: IOException) {
        e.printStackTrace()
    }
    return false
}